<?php

namespace zhtfrontend\components\helpers;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/6
 * Time: 14:35
 */
use common\models\datas\AttributeValue;
use common\components\helpers\Fun;
use common\models\datas\Attribute;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
class Funs
{

    /**
     *  根据类型获取属性
     *  return string
     */

    public static function getAttribute($type_id = 0,$goods_id = 0)
    {
        $html = '';
        $attr = Attribute::find()->where(['type_id'=>$type_id,'is_show'=>1])->all();
        $goodsattr = GoodsAttr::find()->where(['goods_id'=>$goods_id,'obj_type'=>'attribute'])->asArray()->all(Fun::getSiteDb());
        $farr = [];
        if($goodsattr){
            foreach($goodsattr as $g){
                $farr[$g['attribute_id']] =$g;
            }
        }
        if($attr){
            foreach($attr as $val){
                $html .= '<tr>';
                $html .= '<th class="zx-goods-table">'.$val->name.'</th>';
                $html .= '<td >';
                $html .= Html::hiddenInput('attribute['.$val->id.']',$val->name);
                if($val->attr_input_type == 'select'){
                    $attr_value = AttributeValue::find()->where(['attr_id'=>$val->id])->all();
                    $html .= Html::dropDownList('attr_value['.$val->id.']', isset($farr[$val->id])?$farr[$val->id]['value']:'', ArrayHelper::map($attr_value, 'value', 'value'),['prompt'=>'请选择','class'=>'form-control','style'=>'margin-bottom:10px;']);
                }elseif($val->attr_input_type == 'input'){
                    $html .=  Html::textInput('attr_value['.$val->id.']', isset($farr[$val->id])?$farr[$val->id]['value']:'', ['class' => 'input-text']);
                    $html .='<div class="help-block"></div>';
                }else{
                    $html .= Html::textarea('attr_value['.$val->id.']',isset($farr[$val->id])?$farr[$val->id]['value']:'',['class'=>'input-textarea']);
                }
                $html .= '</td>';
                $html .= '</tr>';
            }
        }

        return $html;

    }
    public static function getExtendattributeDetails($type_id=0, $goods_id=0){
        $html = '';
        $attr = Attribute::find()->where(['type_id'=>$type_id])->all();
        $goodsattr = GoodsAttr::find()->where(['goods_id'=>$goods_id])->asArray()->all(Fun::getSiteDb());
        $farr = [];
        if($goodsattr){
            foreach($goodsattr as $g){
                $farr[$g['attribute_id']] = $g;
            }
        }
        if($attr){
            foreach($attr as $val){
                $html .= '<tr>';
                $html .= '<td>'.$val->name.'</td>';
                $html .= '<td>';
                if($val->attr_input_type == 'select'){
                    $attr_value = AttributeValue::find()->where(['attr_id'=>$val->id])->all();
                    $html .= Html::dropDownList('attr_value['.$val->id.']',isset($farr[$val->id])?$farr[$val->id]['value']:'',ArrayHelper::map($attr_value, 'value', 'value'),['prompt'=>'请选择','class'=>'form-control','disabled'=>true]);
                }elseif($val->attr_input_type == 'input'){
                    $html .= isset($farr[$val->id])?$farr[$val->id]['value']:'';
                }else{
                    $html .= isset($farr[$val->id])?$farr[$val->id]['value']:'';
                }
                $html .= '</td>';
                $html .= '</tr>';
            }
        }
        return $html;
    }
    /*获取当前商品的购买数量，如果是课程那就是已预约人数*/
    public static  function getCurrentNum($goods_id){
        $good = Goods::findOne($goods_id);
        $num = 0;
        if(!empty($good)){
            $num = (new \yii\db\Query())
                ->from('{{%orders_items}} oi')
                ->leftJoin('{{%orders}} o','o.id = oi.orders_id')
                ->where(['o.pay_status'=>[0,1,4],'oi.status'=>1,'oi.goods_id'=>$goods_id])
                ->andWhere(['not',['o.status'=>2]])
                ->andWhere(['not',['o.status'=>4]])
                ->count('*',Fun::getSiteDb());
        }
        $num = empty($num)?0:$num;
        return $num;
    }

    //获取商品属性
    public static function getGoodsAttr($goods_id =0){
        $goods_attr = GoodsAttr::find()->where(['goods_id'=>$goods_id])->all();
        $data = [];
        if($goods_attr) {
            foreach ($goods_attr as $attr) {
                $data[$attr['obj_type']] = $attr['value'];
            }
        }
        return $data;
    }

}